﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ElephantSwap
{
    public partial class Form1 : Form
    {
        Elephant lucinda;
        Elephant lloyd;
        Elephant placeholder;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lucinda = new Elephant() { Name = "Lucinda", EarSize = 33 };
            lloyd = new Elephant() { Name = "Lloyd", EarSize = 40 };
        }

        private void button1_Click(object sender, EventArgs e)
        {
            lloyd.WhoAmI();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            lucinda.WhoAmI();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //What went wrong?
            ///
            ///placeholder = new Elephant() { Name = "Placeholder", EarSize = 0 };      !!Here I attempted to create a new object in memory, which isn't necessary!!
            ///lucinda = placeholder;                                                   !!I then move the lucinda pointer to the placeholder object, which drops lucinda's object!!
            ///lloyd = lucinda;                                                         !!I then move the lloyd pointer to where lucinda is pointing, which is the placeholder object!! 
            ///lucinda = lloyd;                                                         !!I then attempt to move lucinda to where lloyd is pointing, concequently it goes nowhere!!
            ///

            // The following is correct for swapping.
            Elephant placeholder;       //Here we simply declare a placeholder variable of type Elephant
            placeholder = lloyd;        //We then move the placeholder pointer to the lloyd object in memory, reserving it and preventing it from being removed by garbage collection.
            lloyd = lucinda;            //We then move the lloyd pointer to where lucinda is pointing, which is the lucinda object in memory.
            lucinda = placeholder;      //We then move the lucinda pointer to where placeholder is pointing, which is the lloyd object in memory.

        }

        private void button4_Click(object sender, EventArgs e)
        {
            lloyd.TellMe("Hi", lucinda);
            lloyd = lucinda;
            lloyd.EarSize = 4321;
            lloyd.WhoAmI();
        }
    }
}
